package cn;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.subject.Subject;

/**
 * <p>功能描述: </p>
 *
 * @author webrx
 * @version 1.0
 * @date 2020-06-05 15:58
 */
@Slf4j
public class Demo {
    public static void main(String[] args) {
        SimpleAccountRealm sar = new SimpleAccountRealm();
        sar.addAccount("zhangsan", "1234");


        DefaultSecurityManager dsm = new DefaultSecurityManager(sar);
        SecurityUtils.setSecurityManager(dsm);
        Subject su = SecurityUtils.getSubject();
        UsernamePasswordToken upt = new UsernamePasswordToken("zhangsan", "1234");
        try {
            su.login(upt);
            if (su.isAuthenticated()) {
                log.info("" + su.isPermitted("user:login"));
            }
        } catch (UnknownAccountException uae) {
            log.info("账号不正确");
        } catch (IncorrectCredentialsException ice) {
            log.info("登录密码不正确");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
    }

}
